<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\Api\controller;
use app\common\controller\Api;

class Product extends Api {

	public function getpro($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_pro_log3.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            if (isset($map->id)) {
                $result = db('product')->where('id', $map->id)->find();
                if ($result) {
                    $this->json_success($result);
                } else {
                    $this->json_error(306);
                }
            }
        } else {
            $this->json_error(304,'token');
        }
	}
    public function pro($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_pro_log1.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n". $content . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $map->content=str_replace('\\','',$content);
        $url = 'yyh2018';
        $token = md5($url);
        //var_dump($map);
        if ($token == $map->token) {
            $map = $this->setpro($map);
            if ($map) {
                $model = \think\Loader::model('Model');
                $list = array();
                $map = array(
                    'status' => array('gt', 0),
                    'extend' => array('gt', 0),
                );
                $list = $model::where($map)->field("name,id,title,icon,'' as 'style'")->select();
                $this->model_id = $model_id = 1;
                $row = db('Model')->select();
                foreach ($row as $key => $value) {
                    $list[$value['id']] = $value;
                }

                if (empty($list[$model_id])) {
                    return $this->error("无此模型！");
                } else {
                    $this->modelInfo = $list[$model_id];
                    if ($this->modelInfo['extend'] > 1) {
                        $this->model = model('Content')->extend($this->modelInfo['name']);
                    } else {
                        $this->model = model('Document')->extend($this->modelInfo['name']);
                    }
                }
                $result =$this->model ->changes($map);
                if ($result) {
                    $this->json_success();
                } else {
                    $this->json_error(306);
                }
            }
        } else {
            $this->json_error(304,'token');
        }
    }
}